package com.hnyfkj.jyindustry.biz.sysmenu.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.entity.SysMemberInfoEntity;
import com.hnyfkj.jyindustry.biz.sysmenu.dao.SysMenuDao;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuInsertDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuTreeDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuTreeListDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuUpdateDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuVoDto;
import com.hnyfkj.jyindustry.biz.sysmenu.entity.SysMenuEntity;
import com.hnyfkj.jyindustry.biz.sysmenu.service.SysMenuService;
import com.hnyfkj.jyindustry.common.exception.HnyfkjBusinessException;
import com.hnyfkj.jyindustry.common.utils.ShiroUtils;
import com.hnyfkj.jyindustry.common.utils.TreeUtil;

/**
 * @author zjq
 *
 * @Description
 *
 * @date 2020年12月10日
 */
@Service("SysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysMenuDao sysMenuDao;


	@Override
	public Map<String, Object> menu(SysMemberInfoEntity sysMemberInfoEntity) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> home = new HashMap<>();
		Map<String, Object> logo = new HashMap<>();
		List<SysMenuEntity> menuList = this.baseMapper.getSystemMenuByMember(sysMemberInfoEntity.getRoleType().toString(),null);
		List<SysMenuVoDto> menuInfo = new ArrayList<>();
		for (SysMenuEntity e : menuList) {
			SysMenuVoDto menuVO = new SysMenuVoDto();
			menuVO.setId(Long.valueOf(e.getMenuId()));
			menuVO.setPid(Long.valueOf(e.getParentId()));
			menuVO.setHref(e.getMenuUrl());
			menuVO.setTitle(e.getMenuName());
			menuVO.setIcon(e.getMenuPicLeave());
			menuVO.setTarget(e.getTarget());
			menuInfo.add(menuVO);
		}
		map.put("menuInfo", TreeUtil.toTree(menuInfo, 0L));
		home.put("title", "首页");
		home.put("href", "sysMenu/welcome");// 控制器路由,自行定义
		logo.put("title", "理发店后台管理");
		logo.put("image", "images/logo.png");// 静态资源文件路径,可使用默认的logo.png
		map.put("homeInfo", home);
		map.put("logoInfo", logo);
		return map;
	}

	@Override
	public JSONObject menuList() {
		List<SysMenuEntity> menuList = sysMenuDao.getSystemMenu(null);
		return getMenuList(menuList);
	}

	public JSONObject getMenuList(List<SysMenuEntity> menuList) {
		List<SysMenuTreeListDto> menuTree = new ArrayList<>();
		for (SysMenuEntity e : menuList) {
			SysMenuTreeListDto menu = new SysMenuTreeListDto();
			menu.setAuthorityId(Long.valueOf(e.getMenuId()));
			menu.setAuthorityName(e.getMenuName());
			menu.setOrderNumber(e.getSort());
			menu.setMenuUrl(e.getMenuUrl());
			menu.setAuthority(e.getPermission());
			menu.setChecked(0);
			menu.setUpdateTime(e.getModifyTime());
			menu.setIsMenu(e.getIsMenu());
			if (e.getParentId() == 0) {
				menu.setParentId(-1);
			} else {
				menu.setParentId(Integer.valueOf(e.getParentId() + ""));
			}
			menuTree.add(menu);
		}
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "");
		json.put("count", menuList.size());
		json.put("data", menuTree);
		return json;
	}

	@Override
	public List<SysMenuEntity> roleMenuList(Integer parentId) {
		return sysMenuDao.getSystemMenu(parentId);
	}

	@Override
	public int insert(final SysMenuInsertDto sysMenuDto) {
		sysMenuDto.setIsMenu(1);
		if (sysMenuDto.getFirstMenu() == -1) {
			if (sysMenuDto.getSecondMenu() == null) {
				sysMenuDto.setParentId(0);
			}
		} else {
			if (sysMenuDto.getSecondMenu() == -1) {
				sysMenuDto.setParentId(sysMenuDto.getFirstMenu());
			} else {
				sysMenuDto.setParentId(sysMenuDto.getSecondMenu());
				sysMenuDto.setIsMenu(0);
			}
		}
		final SysMenuEntity sysMenu = new SysMenuEntity();
		BeanUtils.copyProperties(sysMenuDto, sysMenu);
		checkNameDuplicate(sysMenu);
		sysMenu.setModifyMember((String) ShiroUtils.getSessionAttribute("memberId"));
		sysMenu.setModifyTime(LocalDateTime.now());
		return sysMenuDao.insert(sysMenu);
	}

	@Override
	public int update(SysMenuUpdateDto sysMenuDto) {
		sysMenuDto.setIsMenu(1);
		if (sysMenuDto.getFirstMenu() == -1) {
			if (sysMenuDto.getSecondMenu() == null) {
				sysMenuDto.setParentId(0);
			}
		} else {
			if (sysMenuDto.getSecondMenu() == -1) {
				sysMenuDto.setParentId(sysMenuDto.getFirstMenu());
			} else {
				sysMenuDto.setParentId(sysMenuDto.getSecondMenu());
				sysMenuDto.setIsMenu(0);
			}
		}
		final SysMenuEntity sysMenu = new SysMenuEntity();
		BeanUtils.copyProperties(sysMenuDto, sysMenu);
		checkNameDuplicate(sysMenu);
		sysMenu.setModifyMember((String) ShiroUtils.getSessionAttribute("memberId"));
		sysMenu.setModifyTime(LocalDateTime.now());
		return sysMenuDao.updateById(sysMenu);
	}

	@Override
	public int delectBatch(Integer[] menuIds) {
		for (Integer id : menuIds) {
			deleteByLevel(id);
		}
		return 1;
	}

	private void checkNameDuplicate(SysMenuEntity sysMenu) {
		if (sysMenu.getParentId() != 0) {
			List<SysMenuEntity> sysMenuEntities = sysMenuDao.getSystemMenu(null);
			for (SysMenuEntity entity : sysMenuEntities) {
				if (entity.getMenuName().equals(sysMenu.getMenuName())
						&& entity.getParentId() == sysMenu.getParentId()) {
					throw new HnyfkjBusinessException("名称重复");
				}
			}
		} else {
			List<SysMenuEntity> sysMenuEntities = sysMenuDao.getSystemMenu(Integer.valueOf(sysMenu.getParentId() + ""));
			for (SysMenuEntity entity : sysMenuEntities) {
				if (entity.getMenuName().equals(sysMenu.getMenuName())
						&& entity.getParentId() == sysMenu.getParentId()) {
					throw new HnyfkjBusinessException("名称重复");
				}
			}
		}
	}

	/**
	 * 递归删除父子级菜单后
	 *
	 * @param id
	 */
	private void deleteByLevel(Integer id) {
		List<SysMenuEntity> sysMenuEntities = sysMenuDao.getSystemMenu(id);
		if (!CollectionUtils.isEmpty(sysMenuEntities)) {
			for (SysMenuEntity entity : sysMenuEntities) {
				deleteByLevel(entity.getMenuId());
			}
		}
		sysMenuDao.deleteById(id);
	}

	@Override
	public JSONObject parentIdList(SysMenuEntity sysMenuEntity) {
		List<SysMenuEntity> firstList = sysMenuDao.getSystemMenu(0);
		List<SysMenuEntity> secondList = new ArrayList<SysMenuEntity>();
		Integer firstId = -1;
		Integer secondId = -1;
		if (sysMenuEntity.getParentId() == 0) {
			firstId = sysMenuEntity.getMenuId();
			secondList = sysMenuDao.getSystemMenu(sysMenuEntity.getMenuId());
		} else {
			secondId = sysMenuEntity.getMenuId();
			secondList = sysMenuDao.getSystemMenu(sysMenuEntity.getParentId());
			SysMenuEntity parentMenuEntity = this.getById(sysMenuEntity.getParentId());
			firstId = parentMenuEntity.getMenuId();
		}
		JSONObject json = new JSONObject();
		json.put("firstMenu", firstList);
		json.put("secondMenu", secondList);
		json.put("firstId", firstId);
		json.put("secondId", secondId);
		return json;
	}

	@Override
	public JSONObject menuTreeList(String roleId, Integer isMenu) {
		List<SysMenuEntity> menuList = sysMenuDao.getSystemMenuByMember(roleId, isMenu);
		return getMenuList(menuList);
	}

	@Override
	public List<SysMenuTreeDto> getRoleMenu(String roleId) {
		List<SysMenuEntity> menuList = sysMenuDao.getSystemMenu(null);
		List<SysMenuTreeDto> menuInfo = new ArrayList<>();
		for (SysMenuEntity e : menuList) {
			SysMenuTreeDto menuVO = new SysMenuTreeDto();
			menuVO.setId(Long.valueOf(e.getMenuId()));
			menuVO.setPid(Long.valueOf(e.getParentId()));
			menuVO.setTitle(e.getMenuName());
			menuInfo.add(menuVO);
		}
		List<SysMenuTreeDto> list = TreeUtil.toMenuTree(menuInfo, 0L);
		return list;
	}
}
